//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics;
using System.Windows.Input;
using OpenLS.Chart.UI.Internals;

namespace OpenLS.Chart.UI
{
    ///<summary>
    /// Provides a set of chart related commands
    ///</summary>
    public class ChartCommands
    {
        private static readonly RoutedUICommand[] _internalCommands = new RoutedUICommand[(int) CommandId.Last];


        ///<summary>
        /// Gets the command to set the style of a chart
        ///</summary>
        public static RoutedUICommand SetChartStyle
        {
            get { return ensureCommand(CommandId.SetChartStyle); }
        }

        ///<summary>
        /// Gets the command to format a data series.
        ///</summary>
        public static RoutedUICommand FormatDataSeries
        {
            get { return ensureCommand(CommandId.FormatDataSeries); }
        }

        ///<summary>
        /// Adds the command to unformat a data series
        ///</summary>
        public static RoutedUICommand UnformatDataSeries
        {
            get { return ensureCommand(CommandId.UnformatDataSeries); }
        }

        ///<summary>
        /// Gets the command to format a data point
        ///</summary>
        public static RoutedUICommand FormatDataPoint
        {
            get { return ensureCommand(CommandId.FormatDataPoint); }
        }

        ///<summary>
        /// Gets the command to unformat a data point
        ///</summary>
        public static RoutedUICommand UnformatDataPoint
        {
            get { return ensureCommand(CommandId.UnformatDataPoint); }
        }

        ///<summary>
        /// Gets the command to format the walls of a 3D chart
        ///</summary>
        public static RoutedUICommand FormatWalls
        {
            get { return ensureCommand(CommandId.FormatWalls); }
        }

        ///<summary>
        /// Gets the command to format the floor of a 3D chart
        ///</summary>
        public static RoutedUICommand FormatFloor
        {
            get { return ensureCommand(CommandId.FormatFloor); }
        }

        ///<summary>
        /// Gets the command to format the data labels
        ///</summary>
        public static RoutedUICommand FormatDataLabels
        {
            get { return ensureCommand(CommandId.FormatDataLabels); }
        }

        ///<summary>
        /// Gets the command to format a title
        ///</summary>
        public static RoutedUICommand FormatTitle
        {
            get { return ensureCommand(CommandId.FormatTitle); }
        }

        ///<summary>
        /// Gets the command to add data labels to a chart
        ///</summary>
        public static RoutedUICommand AddDataLabels
        {
            get { return ensureCommand(CommandId.AddDataLabels); }
        }

        ///<summary>
        /// Gets the command to format a plot area
        ///</summary>
        public static RoutedUICommand FormatPlotArea
        {
            get { return ensureCommand(CommandId.FormatPlotArea); }
        }

        ///<summary>
        /// Gets the command to format a chart area
        ///</summary>
        public static RoutedUICommand FormatChartArea
        {
            get { return ensureCommand(CommandId.FormatChartArea); }
        }

        ///<summary>
        /// Gets the command to format the 3D rotation of a chart
        ///</summary>
        public static RoutedUICommand Format3DRotation
        {
            get { return ensureCommand(CommandId.Format3DRotation); }
        }

        ///<summary>
        /// Gets the command to format an axis
        ///</summary>
        public static RoutedUICommand FormatAxis
        {
            get { return ensureCommand(CommandId.FormatAxis); }
        }

        ///<summary>
        /// Gets the command to format a legend
        ///</summary>
        public static RoutedUICommand FormatLegend
        {
            get { return ensureCommand(CommandId.FormatLegend); }
        }

        ///<summary>
        /// Gets the command to format the major grid lines of a chart
        ///</summary>
        public static RoutedUICommand FormatMajorGridLines
        {
            get { return ensureCommand(CommandId.FormatMajorGridLines); }
        }

        ///<summary>
        /// Gets the command to format the minor grid lines of a chart
        ///</summary>
        public static RoutedUICommand FormatMinorGridLines
        {
            get { return ensureCommand(CommandId.FormatMinorGridLines); }
        }

        ///<summary>
        /// Get the command to add major grid lines to a chart
        ///</summary>
        public static RoutedUICommand AddMajorGridLines
        {
            get { return ensureCommand(CommandId.AddMajorGridLines); }
        }

        ///<summary>
        /// Gets the command to add minor grid lines to a chart
        ///</summary>
        public static RoutedUICommand AddMinorGridLines
        {
            get { return ensureCommand(CommandId.AddMinorGridLines); }
        }

       

        /// <summary>
        /// Gets the command to add a title on a chart or axis
        /// </summary>
        public static RoutedUICommand AddTitle
        {
            get { return ensureCommand(CommandId.AddTitle); }
        }
        /// <summary>
        /// Gets the command to add a chart legend
        /// </summary>
        public static RoutedUICommand AddLegend
        {
            get { return ensureCommand(CommandId.AddLegend); }
        }

        private static RoutedUICommand ensureCommand(CommandId commandId)
        {
            return ensureCommand(commandId, commandId.ToString());
        }

        private static RoutedUICommand ensureCommand(CommandId idCommand, string propertyName,
                                                     params InputGesture[] gestures)
        {
            lock (_internalCommands.SyncRoot)
            {
                if (_internalCommands[(int) idCommand] == null)
                {
                    string text = getUIText(idCommand) ?? "!!Missing" + idCommand;
                    var newCommand = new RoutedUICommand(text, propertyName, typeof (ChartCommands));
                    if (gestures != null)
                        foreach (InputGesture gesture in gestures)
                        {
                            newCommand.InputGestures.Add(gesture);
                        }
                    _internalCommands[(int) idCommand] = newCommand;
                }
            }
            return _internalCommands[(int) idCommand];
        }

        private static string getUIText(CommandId idCommand)
        {
            switch (idCommand)
            {
                case CommandId.AddDataLabels:
                    return UIStringTable.AddDataLabels;
                case CommandId.FormatTitle:
                    return UIStringTable.FormatTitle;
                case CommandId.FormatDataLabels:
                    return UIStringTable.FormatDataLabels;
                case CommandId.FormatDataSeries:
                    return UIStringTable.FormatDataSeries;
                case CommandId.UnformatDataSeries:
                    return UIStringTable.UnformatDataSeries;
                case CommandId.FormatDataPoint:
                    return UIStringTable.FormatDataPoint;
                case CommandId.UnformatDataPoint:
                    return UIStringTable.UnformatDataPoint;
                case CommandId.FormatWalls:
                    return UIStringTable.FormatWalls;
                case CommandId.FormatFloor:
                    return UIStringTable.FormatFloor;
                case CommandId.Format3DRotation:
                    return UIStringTable.Format3DRotation;
                case CommandId.FormatChartArea:
                    return UIStringTable.FormatChartArea;
                case CommandId.FormatPlotArea:
                    return UIStringTable.FormatPlotArea;
                case CommandId.SetChartStyle:
                    return UIStringTable.SetChartStyle;
                case CommandId.FormatAxis:
                    return UIStringTable.FormatAxis;
                case CommandId.FormatLegend:
                    return UIStringTable.FormatLegend;
                case CommandId.FormatMajorGridLines:
                    return UIStringTable.FormatMajorGridLines;
                case CommandId.FormatMinorGridLines:
                    return UIStringTable.FormatMinorGridLines;
                case CommandId.AddMajorGridLines:
                    return UIStringTable.AddMajorGridLines;
                case CommandId.AddMinorGridLines:
                    return UIStringTable.AddMinorGridLines;
                case CommandId.AddTitle:
                    return UIStringTable.AddTitle;
                case CommandId.AddLegend:
                    return UIStringTable.AddLegend;
                default:
                    Debug.WriteLine("not implemented " + idCommand);
                    return idCommand.ToString();
            }
        }

        #region Nested type: CommandId

        private enum CommandId
        {
            AddDataLabels,
            FormatTitle,
            FormatDataLabels,
            FormatDataSeries,
            UnformatDataSeries,
            FormatDataPoint,
            UnformatDataPoint,
            FormatFloor,
            FormatWalls,
            FormatPlotArea,
            FormatChartArea,
            Format3DRotation,
            FormatAxis,
            FormatLegend,
            FormatMajorGridLines,
            FormatMinorGridLines,
            AddMajorGridLines,
            AddMinorGridLines,
            AddTitle,
            AddLegend,
            //Delete,
            SetChartStyle,

            Last
        }

        #endregion
    }
}